SUBROUTINE mayorsolution0(nn,xx0,fval)
  USE commonvars
  IMPLICIT NONE
  INCLUDE 'mpif.h'
  
  INTEGER, INTENT(IN)  :: nn
  REAL(8), INTENT(IN)  :: xx0(nn)
  REAL(8), INTENT(OUT) :: fval
  REAL(8)              :: zzprl, fundsl, popsizel, totresl, util, vemax,pcons,zzpu,qcons,ln_relqcons, newsav, newsteal, new_fr_stolen, emaxp,emaxp1,emaxp2,qcons_w_riv,&
                          netW, ipaul, v_slope, multi_slope, betat1_t(MMg), betat2_t(MMg)
  INTEGER              :: imunl, iedul, iterml, ub_sav, ub_netW, isav, timel

  EXTERNAL prodfunc, utilfun_mayor

  !Internalizing global variables
  imunl = imunt
  iedul = iedut
  zzprl = zzprt
  popsizel = popsizeg
  fundsl = fundst
  totresl = totrest
  ipaul = ipaug
  iterml = itermg
  timel = timeg

  !Initialize variables
  util = -infty
  vemax = -infty
  pcons = -infty
  zzpu = -infty
  qcons = -infty
  ln_relqcons = -infty
  emaxp = -infty

  !Transform xx into relevant variables
  IF(flag_sopt_sa == 0) THEN
     newsteal = 0d0
     newsav = (totrest*EXP(xx0(1)) + lbound_sav)/(1.0d0 + EXP(xx0(1)))
  ELSE
     newsteal = 0d0
     newsav = xx0(1)
  END IF

  !Calculating utility
  pcons = totresl - newsav + newsteal

  IF (pcons <= 0.0d0) THEN
!!$     WRITE(*,*) 'ERROR in mayorsolution pcons/qcons',pcons,qcons
     util = bignumber
     fval = infty
!!$     WRITE(*,*) 'fval-inf',fval
     RETURN
  END IF
  
  !We use per-capita public inputs                                                                       |
  zzpu = (fundsl - newsteal)/popsizeg
  IF (zzpu <= small_no) THEN
     qcons = subqcons
     GOTO 672
  END IF
  
  !The output qcons is in per-capita and we account for the degree of rivalry in the utility function
  CALL prodfunc(imunl,iedul,zzpu,zzprl,qcons)

  !We transform per-capita public consumption in total public consumption
  !Below we take into account the degree of rivalry by dividing by popsize**eta
672 qcons = qcons*popsizeg

  !We are making sure that public consumption is not negative; In the simulations at the optimum it is never negative
  IF (qcons <= 0d0) qcons = small_no

  !We transform the level of public consumption to take into account the degree of rivalry
  !This is also the value we output
  qcons_w_riv = qcons/((popsizeg)**eta)
  
  CALL utilfun_mayor(pcons,qcons_w_riv,theta,util)

  IF (qcons <= 0.0d0 .OR. pcons <= 0.0d0) THEN
!!$     WRITE(*,*) 'ERROR in pcons/qcons',pcons,qcons
     util = bignumber
     fval = infty
!!$     WRITE(*,*) 'fval-inf',fval
     RETURN
  END IF

  !Calculating the future value function depends on the history of mayors -- really only relevant for iterm =2 (those who have a history)
  !Case 1: iterm = 1 & iterm = 2 with ipast = 1 (not audited mayors): Construct value function with two different betas because steal=0            
  !Case 2: iterm = 2 & ipast = 2 (audited): Construct value function using betat2

  !Calculating the emaxp [future value] 
  !ln_relqcons corresponds to log public consumption where public consumption is in levels
  !the value function depends on log of public consumption acounting for the degree of rivalry: qconsvec = log(Q/P**eta)
  ln_relqcons = LOG(qcons_w_riv)
  !fine_fun = 0 if newsteal = 0; hence netW = newsav if newsteal = 0
  !We don't have to worry about fine_fun(newsteal) because it is always = 0 as newsteal = 0 always
  netW = newsav
  new_fr_stolen = newsteal/fundsl

  !Find the point on the saving domain where we need to compute the approximated value function
  DO isav = 1, Nwealth
     IF (newsav <  wealth(isav)) THEN
        ub_sav = isav
        EXIT
     END IF
     ub_sav = Nwealth
  END DO
  
  DO isav = 1, Nwealth
     IF (netW <  wealth(isav)) THEN
        ub_netW = isav
        EXIT
     END IF
     ub_netW = Nwealth
  END DO

  IF (Npast > 1) THEN     
     betat1_t = betat1(:)
     betat2_t = betat2(:)
  ELSE
     betat1_t = betat1(:)
     betat2_t = betat1_t
  END IF

  !Constructing emaxp1: value function if future audit = 0; audit changes the betas because it affects the reelection probabilities
  IF(MMg == MMability_steal_q) THEN
     
     !COMPUTE THE LINEAR APPROXIMATION
     IF (ub_sav == 1) THEN
        
        multi_slope = DBLE(CEILING(ABS((newsav - wealth(1)))/inc_wealth))*0.0001d0
        V_slope = (1d0+multi_slope)*(betat1_t(2) - betat1_t(1))/(wealth(2) - wealth(1))
        
     ELSE

        V_slope = (betat1_t(ub_sav) - betat1_t(ub_sav-1))/(wealth(ub_sav) - wealth(ub_sav-1))

     END IF

     ! Since they will not be audited, we don't include newsteal
     ! Public consumption should be in log per-capita
     emaxp1 = betat1_t(MMability_steal_q-Npol_ab-Npol_st-Npol_q-1+1) + betat1_t(MMability_steal_q-Npol_ab-Npol_st-Npol_q-1+2)*ability_g + &
          betat1_t(MMability_steal_q-Npol_ab-Npol_st-Npol_q-1+3)*ability_g**2d0 + betat1_t(MMability_steal_q-Npol_ab-Npol_st-Npol_q-1+5)*ln_relqcons + &
          V_slope*(newsav - wealth(ub_sav)) + betat1_t(ub_sav)

  ELSE
     
     !COMPUTE THE LINEAR APPROXIMATION
     IF (ub_sav == 1) THEN
        
        multi_slope = DBLE(CEILING(ABS((newsav - wealth(1)))/inc_wealth))*0.0001d0
        V_slope = (1d0+multi_slope)*(betat1_t(2) - betat1_t(1))/(wealth(2) - wealth(1))
        
     ELSE
        
        V_slope = (betat1_t(ub_sav) - betat1_t(ub_sav-1))/(wealth(ub_sav) - wealth(ub_sav-1))
        
     END IF
     
     emaxp1 = V_slope*(newsav - wealth(ub_sav)) + betat1_t(ub_sav)

  END IF
     
  !Constructing emaxp2: value function if future audit=1; in this case savings = netW
  IF(MMg == MMability_steal_q) THEN
     
     !COMPUTE THE LINEAR APPROXIMATION
     IF (ub_netW == 1) THEN
        
        multi_slope = DBLE(CEILING(ABS((netW - wealth(1)))/inc_wealth))*0.0001d0
        V_slope = (1d0+multi_slope)*(betat2_t(2) - betat2_t(1))/(wealth(2) - wealth(1))
        
     ELSE
        
        V_slope = (betat2_t(ub_netW) - betat2_t(ub_netW-1))/(wealth(ub_netW) - wealth(ub_netW-1))
        
     END IF
     
     ! Since they will be audited we include newsteal and fine without any dummy for being audited
     emaxp2 = betat2_t(MMability_steal_q-Npol_ab-Npol_st-Npol_q-1+1) + betat2_t(MMability_steal_q-Npol_ab-Npol_st-Npol_q-1+2)*ability_g + &
          betat2_t(MMability_steal_q-Npol_ab-Npol_st-Npol_q-1+3)*ability_g**2d0 + betat2_t(MMability_steal_q-Npol_ab-Npol_st-Npol_q-1+4)*new_fr_stolen + &
          betat2_t(MMability_steal_q-Npol_ab-Npol_st-Npol_q-1+5)*ln_relqcons + V_slope*(netW - wealth(ub_netW)) + betat2_t(ub_netW)

  ELSE
     
     !COMPUTE THE LINEAR APPROXIMATION
     IF (ub_netW == 1) THEN
        
        multi_slope = DBLE(CEILING(ABS((netW - wealth(1)))/inc_wealth))*0.0001d0
        V_slope = (1d0+multi_slope)*(betat2_t(2) - betat2_t(1))/(wealth(2) - wealth(1))
        
     ELSE
        
        V_slope = (betat2_t(ub_netW) - betat2_t(ub_netW-1))/(wealth(ub_netW) - wealth(ub_netW-1))
        
     END IF
     
     emaxp2 = V_slope*(netW - wealth(ub_netW)) + betat2_t(ub_netW)
     
  END IF

  !Constructing total future value: pr(au=0)*f(betat1_t())+ pr(au=1)*[(1-dum_steal)*f(betat2_t()) + dum_steal*f(betat3_t())]
  emaxp = 0d0
  emaxp = paudit_g*emaxp1 + (1d0-paudit_g)*emaxp2

  !Calculating total value [utility+future value]
  vemax = util + betadf*emaxp

  IF(vemax > vemaxoptg) THEN
     vemaxoptg = vemax
     pconsoptg = pcons
     !qconsoptg is public consumption in levels, it DOES NOT takes into account the degree of rivalry
     qconsoptg = qcons
     newsavoptg = newsav
     stealoptg = newsteal
     utiloptg = util
     emaxoptg = emaxp
     zzpuoptg = zzpu
  END IF

  fval = -vemax

!!$  if (myrank == 0 .AND. flag_simulation == 1) THEN
!!$     write(*,'(a,9f16.6)') 'mayor sol 1', vemax, vemaxoptg, newsteal, newsav, pcons, qcons_w_riv, util, emaxp, zzpu
!!$     write(*,'(a,16f14.8)') 'mayor sol 2', betat1_t 
!!$     write(*,'(a,16f14.8)') 'mayor sol 3', betat2_t 
!!$  end if
  
END SUBROUTINE mayorsolution0
